<template>
    <div class="licence-index yuan-container">
        <a-card title="生产许可证">
            <!-- <div class="filter-panel" style="overflow-hidden">
                <div class="filter-list" style="float: right;">
                    <a-button type="primary" @click="add">新增</a-button>
                </div>
            </div> -->
            <div class="table">
                <a-table bordered :rowKey="getRowKey" :data-source="pageList" :columns="columns" :pagination="false"
                    :loading="loading">
                </a-table>
            </div>
        </a-card>
        <a-modal width="600px" :visible="visible" :title="modalOption.title" @ok="modalOption.onOk" @cancel="modalOption.onCancel"
            :okText="modalOption.okText" :cancelText="modalOption.cancelText" class="yuan-modal licence-add-modal">
            <a-spin tip="Loading..." :spinning="formLoading">
                <a-form-model layout="inline" class="yuan-form" ref="form" :model="formData" :rules="rules"
                    :label-col="{ style: { width: '100px' } }" :wrapper-col="{ style: { width: 'calc(100% - 120px)' } }">
                    <div class="form-panel">
                        <div class="form-panel-box yuan-form-col3">
                            <a-form-model-item class="yuan-form-item" label="生产许可证" prop="productionCertificate">
                                <a-input class="yuan-form-input" v-model="formData.productionCertificate"
                                    placeholder="请输入"></a-input>
                            </a-form-model-item>
                        </div>
                    </div>
                </a-form-model>
            </a-spin>
        </a-modal>
    </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { h } from "vue";
import nextApi, { LicenceRes } from "@/api/nextApi";
@Component({
    name: "licenceIndex",
    components: {},
})
export default class extends Vue {
    previewVisible = false;
    imageUrls = ["/img/test.jpg", "/img/test1.png", "../../../assets/images/logo.png"];
    keywords = "";
    loading = false;
    pagination = {
        pageSize: 10,
        current: 1,
        total: 0,
        showTotal: (total: number) => `共${total}条`,
        onChange: this.pageChange,
    };
    pageList: LicenceRes[] = [];
    columns = [
        {
            title: "序号",
            dataIndex: "index",
            key: "index",
            align: "center",
            customRender: (text: string, record: any, index: number) => `${index + 1}`,
        },
        {
            align: "center",
            dataIndex: "productionCertificate",
            key: "productionCertificate",
            title: "生产许可证",
        },
        {
            align: "center",
            dataIndex: "action",
            key: "action",
            title: "操作",
            width: 260,
            customRender: (text: string, record: LicenceRes, index: number) => {
                if (!this.$utilCheckPermission(["productionCertificateCompile"])) return "--";
                return h("div", [
                    h(
                        "a-button",
                        {
                            style: { marginRight: "5px" },
                            props: { icon: "edit", type: "primary", size: "small" },
                            on: { click: () => this.editDetail(record) },
                        },
                        "编辑"
                    ),
                ]);
            },
        },
    ];
    formLoading = false;
    modalOption = {
        title: "新增许可证",
        okText: "保存",
        cancelText: "取消",
        onOk: () => {
            this.modalOk();
        },
        onCancel: () => {
            this.closeModal();
        },
    };
    formData = {};
    rules = {
        productionCertificate: [{ required: true, message: "请输入生产许可证", trigger: "blur" }],
    };
    getRowKey(item: any) {
        return item.id;
    }

    async getList() {
        this.pageList = [];
        try {
            this.loading = true;
            const res = await nextApi.getLicenceList();
            this.pageList = [res.data];
        } finally {
            this.loading = false;
        }
    }
    resetList() {
        this.pagination.current = 1;
        this.getList();
    }

    visible = false;
    editDetail(item: LicenceRes) {
        this.formData = { ...item };
        this.visible = true;
    }
    add() {
        this.formData = {};
        this.visible = true;
    }
    pageChange(page: number, pageSize: number) {
        this.pagination.pageSize = pageSize;
        this.pagination.current = page;
        this.getList();
    }
    mounted() {
        this.getList();
    }
    modalOk() {
        (this.$refs.form as any).validate(async (valid: any) => {
            if (valid) {
                this.formLoading = true;
                try {
                    const res = await nextApi.editLicence(this.formData);
                    this.$message.success(res.msg);
                    this.formData = {};
                    (this.$refs.form as any).resetFields();
                    this.visible = false;
                    this.getList();
                } finally {
                    this.formLoading = false;
                }
            }
        });
    }
    closeModal() {
        this.visible = false;
    }
}
</script>
<style lang="scss">
.licence-index {
    padding: 20px;
}
</style>
